package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.app.Dialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.GoodAdapter;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.GoodsBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.ShopInfoBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.ListListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.CollectionShopModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetCollectionShopFlagModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetGoodsListModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetShopInfoModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.RemoveCollectionShopModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.service.Service;

public class ShopInfoActivity extends BaseActivity implements View.OnClickListener {
    ImageView shopinfo_fh,shopinfo_img;
    TextView shopinfo_name,shopinfo_phone,shopinfo_address,shopinfo_intro;
    String shopid;

    ImageView shopinfo_shoucang;

    RecyclerView shopgoods_rv;
    private List<GoodsBean> items;
    private int shoucangFlag;
    TextView shopinfo_time;

    private Dialog dialog;




    @Override
    void initView() {
        setLayout_file(R.layout.activity_shop_info);
        shopinfo_fh = (ImageView)findViewById(R.id.shopinfo_fh);
        shopinfo_img = (ImageView)findViewById(R.id.shopinfo_img);
        shopinfo_name = (TextView)findViewById(R.id.shopinfo_name);
        shopinfo_phone = (TextView)findViewById(R.id.shopinfo_phone);
        shopinfo_address = (TextView)findViewById(R.id.shopinfo_address);
        shopinfo_intro = (TextView)findViewById(R.id.shopinfo_intro);
        shopgoods_rv = (RecyclerView)findViewById(R.id.shopgoods_rv);
        shopinfo_time = (TextView)findViewById(R.id.shopinfo_time);
        shopinfo_shoucang = (ImageView)findViewById(R.id.shopinfo_shoucang);




        shopid = getIntent().getStringExtra("shopid");
        dialog = new WeiboDialogUtils().createLoadingDialog(ShopInfoActivity.this,"加载中...");

        GetCollectionShopFlagModel getCollectionShopFlagModel = new GetCollectionShopFlagModel();
        getCollectionShopFlagModel.getCollectionShopFlag(getid(),shopid,getCollectionShopFlagListener);


        GetShopInfoModel getShopInfoModel = new GetShopInfoModel();
        getShopInfoModel.getShopInfo(shopid,shopInfoBeanTListener);

        GetGoodsListModel getGoodsListModel = new GetGoodsListModel();
        getGoodsListModel.getGoodsList(shopid,goodsBeanListListener);

    }

    @Override
    void initEvent() {
        shopinfo_fh.setOnClickListener(this);
        shopinfo_shoucang.setOnClickListener(this);

    }


    @Override
    void initDate() {

    }

    TListener<SuccessBean> getCollectionShopFlagListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            //Toast.makeText(ShopInfoActivity.this,successBean.getSuccess(),Toast.LENGTH_SHORT).show();
            if (successBean.getSuccess().equals("1")){
                shopinfo_shoucang.setImageResource(R.mipmap.shoucangl);
                shoucangFlag = 1;//表示已收藏
            }
            else{
                shopinfo_shoucang.setImageResource(R.mipmap.shoucang);
                shoucangFlag = 0;//表示没有收藏
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    ListListener<GoodsBean> goodsBeanListListener = new ListListener<GoodsBean>() {
        @Override
        public void onResponse(List<GoodsBean> list) {
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(ShopInfoActivity.this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            shopgoods_rv.setLayoutManager(linearLayoutManager);
            items=list;
            GoodAdapter goodAdapter=new GoodAdapter(ShopInfoActivity.this,items,R.layout.goods_item);
            shopgoods_rv.setAdapter(goodAdapter);
            shopgoods_rv.setItemAnimator(new DefaultItemAnimator());
            WeiboDialogUtils.closeDialog(dialog);
        }

        @Override
        public void onFail(String msg) {
            WeiboDialogUtils.closeDialog(dialog);
        }
    };

    TListener<ShopInfoBean> shopInfoBeanTListener = new TListener<ShopInfoBean>() {
        @Override
        public void onResponse(ShopInfoBean shopInfoBean) {
            String shopname = shopInfoBean.getShop_name();
            String shopintro = shopInfoBean.getShop_intro();
            String shopaddress = shopInfoBean.getShop_province()+shopInfoBean.getShop_city()+shopInfoBean.getShop_address();
            String shopphone = shopInfoBean.getShop_phone();
            String img = Service.BASE_URL+"flower_impression/public/uploads/"+shopInfoBean.getShop_photo();

            shopinfo_name.setText(shopname);
            shopinfo_intro.setText(shopintro);
            shopinfo_address.setText(shopaddress);
            shopinfo_phone.setText(shopphone);
            shopinfo_time.setText("营业时间："+shopInfoBean.getShop_opentime()+"-"+shopInfoBean.getShop_closetime());
            Picasso.with(ShopInfoActivity.this).load(img).into(shopinfo_img);
        }

            @Override
            public void onFail(String msg) {
                WeiboDialogUtils.closeDialog(dialog);
            }
        };

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.shopinfo_fh:
                    finish();
                    break;
            case R.id.shopinfo_shoucang:
                if (shoucangFlag == 0){
                    CollectionShopModel collectionShopModel = new CollectionShopModel();
                    collectionShopModel.collectionShop(getid(),shopid,collectionShopListener);
                }
                else if (shoucangFlag == 1){
                    RemoveCollectionShopModel removeCollectionShopModel = new RemoveCollectionShopModel();
                    removeCollectionShopModel.removeCollectionShop(getid(),shopid,removeCollectionShoplistener);
                }
                else{
                    Toast.makeText(ShopInfoActivity.this,"收藏错误",Toast.LENGTH_SHORT).show();
                }


        }
    }
    TListener<SuccessBean> removeCollectionShoplistener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            if (successBean.getSuccess().equals("1")){
                Toast.makeText(ShopInfoActivity.this,"取消收藏成功",Toast.LENGTH_SHORT).show();
                shopinfo_shoucang.setImageResource(R.mipmap.shoucang);
                shoucangFlag = 0;
            }
            else{
                Toast.makeText(ShopInfoActivity.this,"错误",Toast.LENGTH_SHORT).show();
                shoucangFlag = 1;
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    TListener<SuccessBean> collectionShopListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean successBean) {
            if (successBean.getSuccess().equals("1")){
                Toast.makeText(ShopInfoActivity.this,"收藏成功",Toast.LENGTH_SHORT).show();
                shopinfo_shoucang.setImageResource(R.mipmap.shoucangl);
                shoucangFlag = 1;
            }
            else{
                Toast.makeText(ShopInfoActivity.this,"错误",Toast.LENGTH_SHORT).show();
                shoucangFlag = 0;
            }


        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(ShopInfoActivity.this,"kk",Toast.LENGTH_SHORT).show();
        }
    };
}
